﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using CoiffeurProject.Screens;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media.Imaging;
using CoiffeurProject.Utils;
using System.Windows.Media;
using CoiffeurProject.DBMS.Entities;
using CoiffeurProject.DBMS.Tables;

namespace CoiffeurProject.UserControls
{
    #region Customer ScrollViewer Entries

    //internal class SVCustomerEntry2
    //{
    //    SVEntry entry;
    //    Image selected, rollover, large_rollover, large_selected;
    //    Label name, phone, address, age, returned;
    //    Customer linkedCustomer;

    //    CustomerScreen screen;

    //    internal SVCustomerEntry2(CustomerScreen screen, Customer customer, int row, int column)
    //    {
    //        bool isPanelViewActive = screen.State == CustomerScreen.ScreenState.CustomerPanel;

    //        entry = new SVEntry(isPanelViewActive ? 364 : 1388, 53, row, column);

    //        this.screen = screen;
    //        this.linkedCustomer = customer;

    //        large_rollover = new Image()
    //        {
    //            Source = new BitmapImage(new Uri(ImagesPath.SelectedCustomer)),
    //            Width = 1388,
    //            Height = 50,
    //            Opacity = 0.7,
    //            Visibility = Visibility.Hidden,
    //            Stretch = Stretch.Fill
    //        };

    //        Grid.SetRow(large_rollover, 0);
    //        Grid.SetColumn(large_rollover, 0);
    //        MainContainer.Children.Add(large_rollover);

    //        large_selected = new Image()
    //        {
    //            Source = new BitmapImage(new Uri(ImagesPath.SelectedCustomer)),
    //            Width = 1388,
    //            Height = 50,
    //            Opacity = 1,
    //            Visibility = Visibility.Hidden,
    //            Stretch = Stretch.Fill
    //        };

    //        Grid.SetRow(large_selected, 0);
    //        Grid.SetColumn(large_selected, 0);
    //        MainContainer.Children.Add(large_selected);

    //        rollover = entry.Rollover;
    //        selected = entry.Selected;

    //        #region Labels

    //        name = new Label()
    //        {
    //            Content = customer.FullName.Length > 17 ? customer.FullName.Substring(0, 14) + "..." : customer.FullName,
    //            FontSize = 24,
    //            HorizontalContentAlignment = HorizontalAlignment.Left,
    //            VerticalContentAlignment = VerticalAlignment.Center,
    //            HorizontalAlignment = HorizontalAlignment.Left,
    //            Margin = new Thickness(10, 0, 0, 0),
    //            Width = screen.State == CustomerScreen.ScreenState.CustomerList ? 230 : 344
    //        };

    //        Grid.SetRow(name, 0);
    //        Grid.SetColumn(name, 0);
    //        MainContainer.Children.Add(name);

    //        phone = new Label()
    //        {
    //            Content = customer.Phone,
    //            FontSize = 24,
    //            HorizontalContentAlignment = HorizontalAlignment.Center,
    //            VerticalContentAlignment = VerticalAlignment.Center,
    //            HorizontalAlignment = HorizontalAlignment.Left,
    //            Margin = new Thickness(240, 0, 0, 0),
    //            Width = 190
    //        };

    //        Grid.SetRow(phone, 0);
    //        Grid.SetColumn(phone, 0);
    //        MainContainer.Children.Add(phone);

    //        address = new Label()
    //        {
    //            Content = customer.Address.Length > 20 ? customer.Address.Substring(0, 17) + "..." : customer.Address,
    //            FontSize = 24,
    //            HorizontalContentAlignment = HorizontalAlignment.Center,
    //            VerticalContentAlignment = VerticalAlignment.Center,
    //            HorizontalAlignment = HorizontalAlignment.Left,
    //            Margin = new Thickness(440, 0, 0, 0),
    //            Width = 230
    //        };

    //        Grid.SetRow(address, 0);
    //        Grid.SetColumn(address, 0);
    //        MainContainer.Children.Add(address);

    //        age = new Label()
    //        {
    //            Content = customer.Age,
    //            FontSize = 24,
    //            HorizontalContentAlignment = HorizontalAlignment.Center,
    //            VerticalContentAlignment = VerticalAlignment.Center,
    //            HorizontalAlignment = HorizontalAlignment.Left,
    //            Margin = new Thickness(680, 0, 0, 0),
    //            Width = 235
    //        };

    //        Grid.SetRow(age, 0);
    //        Grid.SetColumn(age, 0);
    //        MainContainer.Children.Add(age);

    //        returned = new Label()
    //        {
    //            Content = "NP",
    //            FontSize = 24,
    //            HorizontalContentAlignment = HorizontalAlignment.Center,
    //            VerticalContentAlignment = VerticalAlignment.Center,
    //            HorizontalAlignment = HorizontalAlignment.Left,
    //            Margin = new Thickness(930, 0, 0, 0),
    //            Width = 270
    //        };

    //        Grid.SetRow(returned, 0);
    //        Grid.SetColumn(returned, 0);
    //        MainContainer.Children.Add(returned);

    //        #endregion
    //    }

    //    void button_Click(object sender, RoutedEventArgs e)
    //    {
    //        //screen.ChangeSelected(this);
    //        ToggleSelected();
    //    }

    //    void button_MouseLeave(object sender, MouseEventArgs e)
    //    {
    //        switch (screen.State)
    //        {
    //            case CustomerScreen.ScreenState.CustomerPanel:
    //                entry.Rollover = rollover;
    //                break;
    //            default:
    //                entry.Rollover = large_rollover;
    //                break;
    //        }

    //        entry.button_Click(sender, e);
    //    }

    //    void button_MouseEnter(object sender, MouseEventArgs e)
    //    {
    //        switch (screen.State)
    //        {
    //            case CustomerScreen.ScreenState.CustomerPanel:
    //                entry.Rollover = rollover;
    //                break;
    //            default:
    //                entry.Rollover = large_rollover;
    //                break;
    //        }
    //    }

    //    internal void ToggleSelected()
    //    {
    //        switch (screen.State)
    //        {
    //            case CustomerScreen.ScreenState.CustomerPanel:
    //                entry.Selected = selected;
    //                break;
    //            default:
    //                entry.Selected = large_selected;
    //                break;
    //        }

    //        entry.ToggleSelected();
    //    }

    //    internal void UpdateData()
    //    {
    //        name.Content = linkedCustomer.FullName;
    //        phone.Content = linkedCustomer.Phone;
    //        address.Content = linkedCustomer.FormatedAddress;
    //        returned.Content = linkedCustomer.Frequency;
    //        age.Content = linkedCustomer.Age;
    //    }

    //    internal void ScreenStateChanged()
    //    {
    //        rollover.Visibility = Visibility.Hidden;
    //        large_rollover.Visibility = Visibility.Hidden;

    //        switch (screen.State)
    //        {
    //            case CustomerScreen.ScreenState.CustomerList:
    //                name.Width = 230;
    //                name.Content = linkedCustomer.FullName.Length > 17 ? linkedCustomer.FullName.Substring(0, 14) + "..." : linkedCustomer.FullName;
    //                phone.Visibility = Visibility.Visible;
    //                entry.Separator.Width = 1368;
    //                entry.Button.Width = 1368;
    //                entry.MainContainer.Width = 1388;
    //                large_selected.Visibility = selected.Visibility;
    //                selected.Visibility = Visibility.Hidden;
    //                break;
    //            default:
    //                name.Width = 344;
    //                name.Content = linkedCustomer.FullName;
    //                phone.Visibility = Visibility.Hidden;
    //                entry.MainContainer.Width = 364;
    //                entry.Separator.Width = 344;
    //                entry.Button.Width = 344;
    //                selected.Visibility = large_selected.Visibility;
    //                large_selected.Visibility = Visibility.Hidden;
    //                break;
    //        }
    //    }

    //    public Grid MainContainer
    //    {
    //        get { return entry.MainContainer; }
    //    }

    //    public Customer LinkedCustomer
    //    {
    //        get { return linkedCustomer; }
    //        set { linkedCustomer = value; }
    //    }

    //    public bool IsSelected
    //    {
    //        get { return entry.IsSelected; }
    //        set { entry.IsSelected = value; }
    //    }
    //}

    internal class SVCustomerEntry
    {
        Grid main_container;
        Image rollover, large_rollover;
        Image selected, large_selected;
        Button button;
        Label name, phone, address, age, returned;
        Rectangle separator;
        Customer linkedCustomer;

        bool isSelected = false;

        CustomerScreen screen;

        internal SVCustomerEntry(CustomerScreen screen, Customer customer, int row, int column)
        {
            this.screen = screen;
            this.linkedCustomer = customer;

            main_container = new Grid()
            {
                Width = screen.State == CustomerScreen.ScreenState.CustomerList ? 1388 : 364, // 364
                Height = 53,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            Grid.SetRow(main_container, row);
            Grid.SetColumn(main_container, column);

            main_container.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
            main_container.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

            rollover = new Image()
            {
                Source = new BitmapImage(new Uri(ImagesPath.SelectedCustomer)),
                Width = 364,
                Height = 50,
                Opacity = 0.7,
                Visibility = Visibility.Hidden
            };

            Grid.SetRow(rollover, 0);
            Grid.SetColumn(rollover, 0);
            main_container.Children.Add(rollover);

            large_rollover = new Image()
            {
                Source = new BitmapImage(new Uri(ImagesPath.SelectedCustomer)),
                Width = 1388,
                Height = 50,
                Opacity = 0.7,
                Visibility = Visibility.Hidden,
                Stretch = Stretch.Fill
            };

            Grid.SetRow(large_rollover, 0);
            Grid.SetColumn(large_rollover, 0);
            main_container.Children.Add(large_rollover);

            selected = new Image()
            {
                Source = new BitmapImage(new Uri(ImagesPath.SelectedCustomer)),
                Width = 364,
                Height = 50,
                Opacity = 1,
                Visibility = Visibility.Hidden
            };

            Grid.SetRow(selected, 0);
            Grid.SetColumn(selected, 0);
            main_container.Children.Add(selected);

            large_selected = new Image()
            {
                Source = new BitmapImage(new Uri(ImagesPath.SelectedCustomer)),
                Width = 1388,
                Height = 50,
                Opacity = 1,
                Visibility = Visibility.Hidden,
                Stretch = Stretch.Fill
            };

            Grid.SetRow(large_selected, 0);
            Grid.SetColumn(large_selected, 0);
            main_container.Children.Add(large_selected);

            #region Labels

            name = new Label()
            {
                Content = customer.FullName.Length > 17 ? customer.FullName.Substring(0, 14) + "..." : customer.FullName,
                FontSize = 24,
                HorizontalContentAlignment = HorizontalAlignment.Left,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(10, 0, 0, 0),
                Width = screen.State == CustomerScreen.ScreenState.CustomerList ? 230 : 344
            };

            Grid.SetRow(name, 0);
            Grid.SetColumn(name, 0);
            main_container.Children.Add(name);

            phone = new Label()
            {
                Content = customer.Phone,
                FontSize = 24,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(240, 0, 0, 0),
                Width = 190
            };

            Grid.SetRow(phone, 0);
            Grid.SetColumn(phone, 0);
            main_container.Children.Add(phone);

            address = new Label()
            {
                Content = customer.Address.Length > 20 ? customer.Address.Substring(0, 17) + "..." : customer.Address,
                FontSize = 24,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(440, 0, 0, 0),
                Width = 230
            };

            Grid.SetRow(address, 0);
            Grid.SetColumn(address, 0);
            main_container.Children.Add(address);

            age = new Label()
            {
                Content = customer.Age,
                FontSize = 24,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(680, 0, 0, 0),
                Width = 235
            };

            Grid.SetRow(age, 0);
            Grid.SetColumn(age, 0);
            main_container.Children.Add(age);

            returned = new Label()
            {
                Content = "NP",
                FontSize = 24,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(930, 0, 0, 0),
                Width = 270
            };

            Grid.SetRow(returned, 0);
            Grid.SetColumn(returned, 0);
            main_container.Children.Add(returned);

            #endregion

            button = new Button()
            {
                Content = new Image()
                {
                    Source = new BitmapImage(new Uri(ImagesPath.Blank)),
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Stretch = Stretch.Fill
                },
                Width = screen.State == CustomerScreen.ScreenState.CustomerList ? 1388 : 344, // 344
                Height = 40,
            };

            button.MouseEnter += new MouseEventHandler(button_MouseEnter);
            button.MouseLeave += new MouseEventHandler(button_MouseLeave);
            button.Click += new RoutedEventHandler(button_Click);
            button.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            Grid.SetRow(button, 0);
            Grid.SetColumn(button, 0);
            main_container.Children.Add(button);

            separator = new Rectangle()
            {
                Width = screen.State == CustomerScreen.ScreenState.CustomerList ? 1388 : 344,
                Height = 1,
                Opacity = 1,
                Fill = new SolidColorBrush(Colors.Gray)
            };

            Grid.SetRow(separator, 1);
            Grid.SetColumn(separator, 0);
            main_container.Children.Add(separator);
        }

        void button_Click(object sender, RoutedEventArgs e)
        {
            screen.ChangeSelected(this);
            ToggleSelected();
        }

        void button_MouseLeave(object sender, MouseEventArgs e)
        {
            switch (screen.State)
            {
                case CustomerScreen.ScreenState.CustomerPanel:
                    rollover.Visibility = Visibility.Hidden;
                    break;
                default:
                    large_rollover.Visibility = Visibility.Hidden;
                    break;
            }
        }

        void button_MouseEnter(object sender, MouseEventArgs e)
        {
            switch (screen.State)
            {
                case CustomerScreen.ScreenState.CustomerPanel:
                    rollover.Visibility = Visibility.Visible;
                    break;
                default:
                    large_rollover.Visibility = Visibility.Visible;
                    break;
            }
        }

        internal void ToggleSelected()
        {
            switch (screen.State)
            {
                case CustomerScreen.ScreenState.CustomerPanel:
                    selected.Visibility = isSelected ? Visibility.Hidden : Visibility.Visible;
                    break;
                default:
                    large_selected.Visibility = isSelected ? Visibility.Hidden : Visibility.Visible;
                    break;
            }

            isSelected = !isSelected;
        }

        internal void UpdateData()
        {
            name.Content = linkedCustomer.FullName;
            phone.Content = linkedCustomer.Phone;
            address.Content = linkedCustomer.FormatedAddress;
            returned.Content = linkedCustomer.Frequency;
            age.Content = linkedCustomer.Age;
        }

        internal void ScreenStateChanged()
        {
            rollover.Visibility = Visibility.Hidden;
            large_rollover.Visibility = Visibility.Hidden;

            switch (screen.State)
            {
                case CustomerScreen.ScreenState.CustomerList:
                    name.Width = 230;
                    name.Content = linkedCustomer.FullName.Length > 17 ? linkedCustomer.FullName.Substring(0, 14) + "..." : linkedCustomer.FullName;
                    phone.Visibility = Visibility.Visible;
                    separator.Width = 1368;
                    button.Width = 1368;
                    main_container.Width = 1388;
                    large_selected.Visibility = selected.Visibility;
                    selected.Visibility = Visibility.Hidden;
                    break;
                default:
                    name.Width = 344;
                    name.Content = linkedCustomer.FullName;
                    phone.Visibility = Visibility.Hidden;
                    main_container.Width = 364;
                    separator.Width = 344;
                    button.Width = 344;
                    selected.Visibility = large_selected.Visibility;
                    large_selected.Visibility = Visibility.Hidden;
                    break;
            }
        }

        public Grid MainContainer
        {
            get { return main_container; }
        }

        public Customer LinkedCustomer
        {
            get { return linkedCustomer; }
            set { linkedCustomer = value; }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set { isSelected = value; }
        }
    }


    #endregion

    internal class SVCustomerNotEntry
    {
        Grid main_container;
        Label notification_text;
        Rectangle separator;
        Birthday linkedNotification;

        CustomerScreen screen;

        internal SVCustomerNotEntry(CustomerScreen screen, Birthday notification, int row, int column)
        {
            this.screen = screen;
            this.linkedNotification = notification;

            main_container = new Grid()
            {
                Width = 345,
                Height = 36,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            Grid.SetRow(main_container, row);
            Grid.SetColumn(main_container, column);

            main_container.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(35) });
            main_container.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

            #region Labels

            notification_text = new Label()
            {
                Content = notification.Description.Length > 35 ? notification.Description.Substring(0,33) + "..." : notification.Description,
                FontSize = 18,
                HorizontalContentAlignment = HorizontalAlignment.Left,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = new Thickness(3, 0, 0, 0),
                Width = 340,
                Height = 35,
            };

            Grid.SetRow(notification_text, 0);
            Grid.SetColumn(notification_text, 0);
            main_container.Children.Add(notification_text);

            #endregion

            separator = new Rectangle()
            {
                Width = 345,
                Height = 1,
                Opacity = 1,
                Fill = new SolidColorBrush(Colors.Gray)
            };

            Grid.SetRow(separator, 1);
            Grid.SetColumn(separator, 0);
            main_container.Children.Add(separator);
        }

        public Grid MainContainer
        {
            get { return main_container; }
        }

        public Birthday LinkedCustomer
        {
            get { return linkedNotification; }
            set { linkedNotification = value; }
        }
    }

}
